//11.1 - MineField - Mark Lee - Prima Publishing #include #include #include #include #include #include #include "MenuUtility.h" using namespace std; using namespace menuNamespace; class StepOnMine{}; class FailedDisarm{}; class MineField { vector minefield; //stores where the player has been vector beenThere; int location; //current location of player public: MineField() //4X4 minefield { srand(time(0)); location = 0; for (int c = 0; c <16; c++) { minefield.push_back(false); beenThere.push_back(false); } for (int i = 0; i <10; i++) //place 10 random mines minefield[rand()%15+1] = true; beenThere[0] = true; } bool IsAMine(int location) { return minefield[location]; } string draw() { string s; for (int i = 0; i <4; i++) { for (int c=0; c<4; c++) { if (location == i*4+c) s+='P'; else { if (beenThere[i*4+c]) s+= "X"; else s+= " "; } s+="|"; } s+= '\n'; } return s; } bool moreMines() { for (int i=0; i<16; i++) if (beenThere[i]) return true; return false; } int Directions() { string options[4]; options[0] = "North"; options[1] = "East"; options[2] = "South"; options[3] = "West"; return menu(options, 4); } int& place() {return location;} void goThere(int place) {beenThere[place] = true;} }; void Detonate() { cout << "You detonated the mine. Ka-boom!!!\n"; } void disarm() { int temp = rand()%2+1; if (temp-1) throw FailedDisarm(); } int main (void) { set_terminate(Detonate); MineField m; //create the minefield string input; cout << "Welcome to the MineField!!\n" << "You are part of the elite Soviet mine team " <<"XJ77,\n sent to clear a deadly minefield full" <<" of remote heat-sensing claymore mines.\n" << "Most of your team will not survive.\n" << "Only the best of you will see then end of " << " the day.\n Do you have what it takes?\n"; cin >> input; if(input == "no" || input == "No") goto TOO_BAD; cout << "You are in the NorthWest corner.\n"; PLAY: try { int goTo; while(m.moreMines()){ cout << endl << m.draw(); cout << "Your position is marked with a P.\n" << "Which direction would you like to go?" <3) proper = true; if (goTo == 2 && (m.place()-3)%4 != 0) proper = true; if (goTo == 3 && m.place() <12) proper = true; if (goTo == 4 && m.place()%4 != 0) proper = true; if (!proper) cout<<"\nYou cannot go that way.\n"; }while (!proper); if (goTo == 1) m.place() -= 4; if (goTo == 2) m.place()++; if (goTo == 3) m.place() += 4; if (goTo == 4) m.place()--; m.goThere(m.place()); if (m.IsAMine(m.place())) throw StepOnMine(); } } catch(StepOnMine) { int input; do { cout << "\nYou have encountered a mine.\n" << "What would you like to do?\n"\ << "[1]Attempt to Disarm it.\n" << "[2]Run Away.\n"; cin >> input; }while(input <1 && input >2); if (input == 1) { try { disarm(); } catch(FailedDisarm) {terminate();} cout << "You have disarmed the mine!!!\n"; goto PLAY; } cout << "You have failed the XJ77 team.\n"; goto TOO_BAD; } return 0; TOO_BAD: cout << "\nMaybe next year kid.\n"; return 0; }